package han.cirno.corrupt.modules.task.add

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import han.cirno.corrupt.R
import han.cirno.corrupt.data.instances.UserTaskData.TaskTag
import han.cirno.corrupt.databinding.DialogTaskChipEditBinding
import han.cirno.corrupt.dialog.BottomPopDialog

class ChipEditDialog(context: Context, successCallback: (TaskTag)->Unit) : BottomPopDialog<DialogTaskChipEditBinding>(context,
    DialogTaskChipEditBinding.inflate(LayoutInflater.from(context))){
    init {
        viewBinding.submitButton.setOnClickListener{
            val colorHexString=viewBinding.dialogChipEditInputColor.text.toString()
            val color:Int
            try{
                color=Color.parseColor(colorHexString)
            }catch (e:Exception){
                viewBinding.dialogChipEditInputColor.error=context.getText(R.string.dialog_chip_add_error_color)
                return@setOnClickListener
            }
            val name=viewBinding.dialogChipEditInputName.text.toString()
            if(name.isEmpty()){
                viewBinding.dialogChipEditInputName.error=context.getText(R.string.dialog_chip_add_error_name)
                return@setOnClickListener
            }
            val taskTag=TaskTag(name,color)
            successCallback(taskTag)
            dismiss()
        }
        viewBinding.discardButton.setOnClickListener{ dismiss() }
    }
}